import {ajaxGetDictItems, getMoreDictItems} from '@/api/api'
import {
  HOUSE_REGION_DICT,
  HOUSE_STRUCTURE_DICT,
  HOUSE_SELL_TYPE,
  HOUSE_PRICE_UNIT
} from "@/store/mutation-types"

const dictionary = {
  state: {
    regionDict: null,
    structureDict: null,
    houseSellType: null,
    housePriceUnit: null
  },

  mutations: {
    SET_REGION_DICT: (state, regionDict) => {
      state.regionDict = regionDict
    },
    SET_STRUCTURE_DICT: (state, structureDict) => {
      state.structureDict = structureDict
    },
    SET_HOUSE_SELL_TYPE: (state, houseSellType) => {
      state.houseSellType = houseSellType
    },
    SET_HOUSE_PRICE_UNIT: (state, housePriceUnit) => {
      state.housePriceUnit = housePriceUnit
    }
  },

  actions: {
    GetRegionDict({ commit }) {
      return new Promise((resolve, reject) => {
        ajaxGetDictItems('house_region_type').then(response => {
          commit('SET_REGION_DICT', response.result)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    GetStructureDict({ commit }) {
      return new Promise((resolve, reject) => {
        ajaxGetDictItems('house_structure_type').then(response => {
          commit('SET_STRUCTURE_DICT', response.result)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    GetHouseSellType({ commit }) {
      return new Promise((resolve, reject) => {
        ajaxGetDictItems('house_sell_type').then(response => {
          commit('SET_HOUSE_SELL_TYPE', response.result)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    GetHousePriceUnit({ commit }) {
      return new Promise((resolve, reject) => {
        ajaxGetDictItems('house_price_unit').then(response => {
          commit('SET_HOUSE_PRICE_UNIT', response.result)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    GetMoreDictItems({ commit }) {
      return new Promise((resolve, reject) => {
        getMoreDictItems([HOUSE_PRICE_UNIT, HOUSE_SELL_TYPE, HOUSE_STRUCTURE_DICT, HOUSE_REGION_DICT]).then(response => {
          if(response.success) {
            commit('SET_HOUSE_PRICE_UNIT', response.result.house_price_unit)
            commit('SET_HOUSE_SELL_TYPE', response.result.house_sell_type)
            commit('SET_STRUCTURE_DICT', response.result.house_structure_type)
            commit('SET_REGION_DICT', response.result.house_region_type)
          }
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }

}

export default dictionary